// 引入axios
import axios from 'axios'
// 请求延时（毫秒数，如果请求话费超过了'timeout'的时间，请求将被中断）
axios.defaults.timeout = 100000

const api_name = 'http://localhost:8333/home/homeFlower'

// 添加一个请求拦截器
axios.interceptors.request.use(function (config) {
    // 在请求发送之前，将 token 放入请求头中
    const token = localStorage.getItem('authorization');
    if (token) {
      config.headers.Authorization = `${token}`;
    }
    return config;
  }, function (error) {
    return Promise.reject(error);
  });


export default {

    //获得所有
    getAllFlower() {
        return axios.get(`${api_name}/` + "getAllFlower") 
    },

    //更新
    updateFlower(editFlower) {
        return axios.put(`${api_name}/` + "update",{
            id: editFlower.id,
            flowerName: editFlower.flowerName,
            pic: editFlower.pic,
            details: editFlower.details,
            conserve: editFlower.conserve,
            family: editFlower.family
        })
    },

    //删除
    remove(id) {
        return axios.delete(`${api_name}/` + "remove" + `/${id}`)
    },

    //获取flower信息
    getFlowerById(id) {
        return axios.get(`${api_name}/` + "get" + `/${id}`)
    },

    //分页
    pageList(page, limit, searchObj) {
        return axios.post(`${api_name}/${page}/${limit}`,{
            flowerName: searchObj.flowerName,
            details: searchObj.defaults,
            family: searchObj.family
        })
    },

    //增加
    addFlower(saveObj) {
        return axios.post(`${api_name}/` + "save", {
            flowerName: saveObj.flowerName,
            pic: saveObj.pic,
            details: saveObj.details,
            conserve: saveObj.conserve,
            family: saveObj.family
        })
    },
}

